﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Business;
using H2.NET.Data.Common;

namespace H2.NET.Web.Pages
{
    public partial class HShipTo : TaskControl
    {
        #region 属性 Public
        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }

        }
        
        
        public Anthem.DropDownList Actions
        {
            get
            {
                return (Page  as PHCMD).Actions;

            }
        }

        public Anthem.CheckBoxList Keys
        {
            get
            {
                return (Page as PHCMD).Keys;

            }
        }

        public List<hcmd_ShipTo> ShipTos
        {
            get
            {
                if (StoreAPI["TabShipTo~" + ID] == null)
                {
                    StoreAPI["TabShipTo~" + ID] = new List<hcmd_ShipTo>();
                }

                return (List<hcmd_ShipTo>)StoreAPI["TabShipTo~" + ID];
            }
            set { StoreAPI["TabShipTo~" + ID] = value; }
        }

        private bool IsUsed
        {
            get
            {
                return ViewState["IsUsed"] == null ? false : (bool)ViewState["IsUsed"];
            }
            set { ViewState["IsUsed"] = value; }
        }
        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitDropDownList();

                if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit)
                {
                    SetColor();
                    if (ShipTos.Count == 0)
                    {
                        ShipTos.Add(new hcmd_ShipTo());
                    }
                }

                if (!TaskAPI.IsInitiate)
                {
                    ShipTos.Clear();
                    ShipTos = (List<hcmd_ShipTo>)(hcmd_ShipTo.process == TaskAPI.Process & hcmd_ShipTo.incident == TaskAPI.Incident).List<hcmd_ShipTo>();
                    if (ShipTos.Count == 0)
                    {
                        ShipTos.Add(new hcmd_ShipTo());
                    }
                    SheetIndex = 0;
                    Distribute(SheetIndex);
                }

                Sheets.Items.Clear();
                for (int i = 0; i < ShipTos.Count; i++)
                {
                    Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }

                SheetsCount.Text = ShipTos.Count.ToString();

                #region 设置控件禁启用
                bool flg = (TaskAPI.IsBeginStep  && TaskAPI.IsAllowSubmit) ? true : false;
                SetEnable(flg, ST_Sold_To_No, ST_Sold_To_Name,  ST_Division, ST_Ship_To_No, ST_Project_Term, ST_Country,ST_Attributes1, ST_City, ST_Address, ST_Contact_Person, ST_Tel);
                SetVisible(flg, Add, Delete);
                SetVisible(flg, Add, Delete);
                #endregion

                SetDefaultValue();
                IsLoaded = true;

                if (!flg || !TaskAPI.IsAllowSubmit)
                {
                    foreach (Control cl in this.Controls)
                    {
                        if (cl is TextBox)
                        {
                            TextBox tb = (TextBox)cl;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                tb.ReadOnly = true;
                                tb.ForeColor = System.Drawing.Color.DimGray;
                            }
                        }
                    }
                    foreach (Control cr in this.Controls)
                    {
                        if (cr is DropDownList)
                        {
                            DropDownList tb = (DropDownList)cr;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                tb.ForeColor = System.Drawing.Color.DimGray;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region 事件 Sheets 选择
        protected void Sheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SheetIndex != -1)
            {
                Collect(SheetIndex);
            }

            if (Sheets.SelectedIndex != -1)
            {
                Distribute(Sheets.SelectedIndex);
                SheetIndex = Sheets.SelectedIndex;
            }

            //Color Change
            ST_Division_SelectedIndexChanged(null, e);
        }
        #endregion

        #region 事件 Sheets 增加
        protected void Add_Click(object sender, ImageClickEventArgs e)
        {
            AddNewShipTo();
        }

        private void AddNewShipTo()
        {
            Sheets.Items.Add(new ListItem((Sheets.Items.Count + 1).ToString(), (Sheets.Items.Count + 1).ToString()));
            Collect(SheetIndex);

            hcmd_ShipTo st = CopyFrom(ShipTos[SheetIndex]);
            st.Process = TaskAPI.Process;
            st.Incident = TaskAPI.Incident;
            st.UserName = UserAPI.UserName;
            st.UserFullName = UserAPI.UserFullName;
            st.Status = 1;
            ShipTos.Insert(SheetIndex+1, st);
            SheetsCount.Text = Sheets.Items.Count.ToString();
        }
        #endregion

        #region 事件 Sheets 删除
        protected void Delete_Click(object sender, ImageClickEventArgs e)
        {
            if (Sheets.SelectedIndex != -1)
            {
                if (Sheets.Items.Count == 1)
                {
                    Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                    return;
                }
                //Return 删除SHIP
                foreach (hcmd_ShipTo st in ShipTos)
                {
                    if(st.Incident==TaskAPI.Incident && st.Sheets==this.Sheets.SelectedValue.ToString())
                    {
                        Storage.Delete(st);
                    }
                }
                //
                ShipTos.RemoveAt(Sheets.SelectedIndex);
                Sheets.Items.Clear();
                for (int i = 0; i < ShipTos.Count; i++)
                {
                    Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }

                Sheets.SelectedIndex = 0;
                SheetIndex = 0;
                Distribute(SheetIndex);
                SheetsCount.Text = Sheets.Items.Count.ToString();
               
            }
        }

        #endregion

        #region 事件 Actions
        protected void Actions_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetColor();
        }
        #endregion

        #region 事件 Ship_To_No、ST_Ship_To_Name赋值给实体类和页面值
        protected void ST_Ship_To_No_TextChanged(object sender, EventArgs e)
        {
            //SetEnable(string.IsNullOrEmpty((sender as TextBox).Text), ST_Sold_To_Name, ST_Sold_To_Name_2,  ST_Sales_Office, ST_Region, ST_City_Code);
            //m_Customer cc = (m_Customer.customerCode == (sender as TextBox).Text).ListFirst<m_Customer>(m_Customer.division.Asc);
            //if (cc != null)
            //{
            //    ST_Sold_To_No.Text     = cc.CustomerCode;
            //    ST_Sold_To_Name.Text   = cc.CustomerName;
            //    ST_Sold_To_Name_2.Text = cc.CustomerName;
            //    ST_Ship_To_Name2.Text  = cc.CustomerName;
            //    ST_SD_Sales_Office.Text= cc.SalesOffice;
            //    ST_SD_Region.Text      = cc.RegionID;
            //    ST_SD_Division.Text    = cc.Division;
            //    ST_SD_County_Code.Text = cc.CountyCode;

            //    if (ST_Sold_To_Name.Text == "")
            //    {
            //        ST_Ship_To_Name2.Text = "";
            //    }
                
                
                //foreach(ListItem li in ST_Sales_Office.Items)
                //{
                //    if(li.Value.IndexOf(cc.SalesOffice) != -1)
                //    {
                //        ST_Sales_Office.SelectedValue = li.Value;
                //        ST_Sales_Office_SelectedIndexChanged(null, e);
                //        break;
                //    }
                //}

                //foreach (ListItem li in ST_Region.Items)
                //{
                //    if (li.Value.IndexOf(cc.RegionID) != -1)
                //    {
                //        ST_Region.SelectedValue = li.Value;
                //        ST_Region_SelectedIndexChanged(null, e);
                //        break;
                //    }
                //}

                //foreach (ListItem li in ST_City_Code.Items)
                //{
                //    if (li.Value.IndexOf(cc.CityID) != -1)
                //    {
                //        ST_City_Code.SelectedValue = li.Value;
                //        ST_County_TextChanged(null, e);
                //        break;
                //    }
                //}
            //}
            //else
            //{ 
            //    ST_Sold_To_Name.Text   = "";
            //    ST_Sold_To_Name_2.Text = "";
            //}

            ST_Ship_To_Name_TextChanged(null, e);

            foreach (hcmd_ShipTo st in ShipTos)
            {
                st.ST_Sold_To_No = ST_Sold_To_No.Text;
                st.ST_Sold_To_Name = ST_Sold_To_Name.Text;
                //st.ST_Sold_To_Name_2 = ST_Sold_To_Name_2.Text;
                st.ST_Sold_To_Name = ST_Sold_To_Name.Text;
                st.ST_Division = ST_Division.SelectedValue;
                //st.ST_Sales_Office = ST_Sales_Office.SelectedValue;
                st.ST_Region = ST_Region.Text;
                st.ST_City_Code = ST_City_Code.Text;
            }
        }

        protected void ST_Ship_To_Name_TextChanged(object sender, EventArgs e)
        {
            switch (ST_Division.SelectedValue)
            {
                case "K1":
                case "K3":
                    ST_Ship_To_Name.Text = ST_Address.Text + "-" + ST_Sold_To_No.Text;
                break;
                case "K2":
                ST_Ship_To_Name.Text = ST_Project_Term.Text + "-" + ST_Address.Text + "-" + ST_Sold_To_No.Text;
                break;
            }
        }
        #endregion

        //#region 事件 客户名称1=客户名称2
        //protected void ST_Sold_To_Name_TextChanged(object sender, EventArgs e)
        //{
        //    if (ST_Sold_To_Name.Text != "")
        //    {
        //        ST_Sold_To_Name_2.Text = ST_Sold_To_Name.Text;
        //        ST_Ship_To_Name2.Text = ST_Sold_To_Name.Text;
        //    }
        //    else
        //    {
        //        ST_Sold_To_Name_2.Text = "";
        //        ST_Ship_To_Name2.Text = "";
        //    }
        //}
        //#endregion

        #region 事件 Attributes3/Attributes4/ProjectTerm/PartialDeliveryPerItem/STMaxPartialDeliveries
        protected void ST_Division_SelectedIndexChanged(object sender, EventArgs e)
        {
            ST_Ship_To_Name.Text = "";
           
            #region 事件 Attributes3/Attributes4/ProjectTerm
            if (Actions.SelectedValue == "1" && ST_Division.SelectedValue == "K2")
            {
                SetForeColor(Color.Blue,  ST_Project_Term_L);
            }
            else
            {
                SetForeColor(Color.Black, ST_Project_Term_L);

            }
            #endregion

            //#region 事件 PartialDeliveryPerItem/STMaxPartialDeliveries
            //if (ST_Division.SelectedValue == "K3")
            //{
            //    ST_Partial_Delivery_Per_Item.Text = "A";
            //}
            //else if (ST_Division.SelectedValue == "" )
            //{
            //    ST_Partial_Delivery_Per_Item.Text = "";
            //}
            //else
            //{
            //    ST_Partial_Delivery_Per_Item.Text = "0";
            //}
             
            //if (ST_Division.SelectedValue == "K3")
            //{
            //    ST_Max_Partial_Deliveries.Text = "1";
            //}
            //else if (ST_Division.SelectedValue == "")
            //{
            //    ST_Max_Partial_Deliveries.Text = "";
            //}
            //else
            //{
            //    ST_Max_Partial_Deliveries.Text = "9";
            //}
            //#endregion

            //#region 河南百安居

            //if (ST_Division.SelectedValue == "K3" && ST_Hierarchy_2.SelectedValue == "121842" && ST_Region.SelectedValue == "编号_180_河南")
            //{

            //    IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
            //     | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
            //    foreach (m_Sales_Office BC in bcc)
            //    {
            //        ST_County.Text = BC.County_Code;
            //    }
            //    ST_Transportation_Zone.Text = "CNSHDJ0000";
            //}

            //else
            //{
            //    IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
            //     | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
            //    foreach (m_Sales_Office BC in bcc)
            //    {
            //        ST_County.Text = BC.County_Code;
            //        ST_Transportation_Zone.Text = BC.Transportation_Zone;
            //    }
            //}
            //#endregion

            ST_Ship_To_Name_TextChanged(null, e);
        }
        #endregion

        //#region 事件 Sales Office-region
        //protected void ST_Sales_Office_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ST_City_Code.Items.Clear();
        //    //ST_County.Text = "";
        //    ST_Transportation_Zone.Text = "";
        //    ST_Region.Items.Clear();
        //    ST_Region.Items.Add(new ListItem("", ""));
        //    IList<m_Sales_Office> srs = (m_Sales_Office.status == 1 & (m_Sales_Office.sales_Office == ST_Sales_Office.SelectedValue
        //     | m_Sales_Office.sales_Office == "")).List<m_Sales_Office>();
        //    foreach (m_Sales_Office SR in srs)
        //    {
        //        if (ST_Region.Items.FindByValue(SR.Region_Code_Name) == null)
        //        {
        //            ST_Region.Items.Add(new ListItem(SR.Region_Code_Name, SR.Region_Code_Name));
        //        }

        //    }
            
        //}
        //#endregion

        //#region 事件 Region-city
        //protected void ST_Region_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ST_County.Text = "";
        //    ST_Transportation_Zone.Text = "";
        //    ST_City_Code.Items.Clear();
        //    ST_City_Code.Items.Add(new ListItem("", ""));
        //    IList<m_Sales_Office> scc = (m_Sales_Office.status == 1 & (m_Sales_Office.region_Code_Name == ST_Region.SelectedValue
        //        | m_Sales_Office.region_Code_Name == "")).List<m_Sales_Office>();
        //    foreach (m_Sales_Office SC in scc)
        //    {
        //        if (ST_City_Code.Items.FindByValue(SC.City_Code_Name) == null)
        //        {
        //            ST_City_Code.Items.Add(new ListItem(SC.City_Code_Name, SC.City_Code_Name));

        //        }
        //    }

            //#region 河南百安居

            //if (ST_Division.SelectedValue == "K3" && ST_Hierarchy_2.SelectedValue == "121842" && ST_Region.SelectedValue == "编号_180_河南")
            //{

            //    IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
            //     | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
            //    foreach (m_Sales_Office BC in bcc)
            //    {
            //        ST_County.Text = BC.County_Code;
            //    }
            //    ST_Transportation_Zone.Text = "CNSHDJ0000";
            //}

            //else
            //{
            //    IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
            //     | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
            //    foreach (m_Sales_Office BC in bcc)
            //    {
            //        ST_County.Text = BC.County_Code;
            //        ST_Transportation_Zone.Text = BC.Transportation_Zone;
            //    }
            //}
            //#endregion
        
       

        //#region 事件 CountyCode—TransportationZone
        //protected void ST_County_TextChanged(object sender, EventArgs e)
        //{
        //    IList<m_Sales_Office> sct = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
        //         | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
        //    foreach (m_Sales_Office SC in sct)
        //    {
        //        ST_County.Text = SC.County_Code;
        //        ST_Transportation_Zone.Text = SC.Transportation_Zone;
        //    }
        //}
        //#endregion

        //#region 事件 ST_Hierarchy_2、ST_City_Code
        //protected void ST_Hierarchy_2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    #region 河南百安居

        //    if (ST_Division.SelectedValue == "K3" && ST_Hierarchy_2.SelectedValue == "121842" && ST_Region.SelectedValue == "编号_180_河南")
        //    {

        //        IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
        //         | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
        //        foreach (m_Sales_Office BC in bcc)
        //        {
        //            ST_County.Text = BC.County_Code;
        //        }
        //        ST_Transportation_Zone.Text = "CNSHDJ0000";
        //    }

        //    else
        //    {
        //        IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
        //         | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
        //        foreach (m_Sales_Office BC in bcc)
        //        {
        //            ST_County.Text = BC.County_Code;
        //            ST_Transportation_Zone.Text = BC.Transportation_Zone;
        //        }
        //    }
        //    #endregion
        //}

        //protected void ST_City_Code_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    #region 河南百安居

        //    if (ST_Division.SelectedValue == "K3" && ST_Hierarchy_2.SelectedValue == "121842" && ST_Region.SelectedValue == "编号_180_河南")
        //    {

        //        IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
        //         | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
        //        foreach (m_Sales_Office BC in bcc)
        //        {
        //            ST_County.Text = BC.County_Code;
        //        }
        //        ST_Transportation_Zone.Text = "CNSHDJ0000";
        //    }

        //    else
        //    {
        //        IList<m_Sales_Office> bcc = (m_Sales_Office.status == 1 & (m_Sales_Office.city_Code_Name == ST_City_Code.SelectedValue
        //         | m_Sales_Office.city_Code_Name == "")).List<m_Sales_Office>();
        //        foreach (m_Sales_Office BC in bcc)
        //        {
        //            ST_County.Text = BC.County_Code;
        //            ST_Transportation_Zone.Text = BC.Transportation_Zone;
        //        }
        //    }
        //    #endregion
        //}
        //#endregion

        #region 方法 Distribute、Collect 对页面和实体类赋值
        public void Distribute(int Index)
        {
            if (Index == -1 || ShipTos.Count == 0)
            {
                return;
            }

            bool flg = (TaskAPI.IsBeginStep  && TaskAPI.IsAllowSubmit) ? true : false;

            hcmd_ShipTo st = ShipTos[Index];

            ST_Sold_To_No.Text              = st.ST_Sold_To_No;
            //ST_Total_ST.Text                = st.ST_Total_ST;
            ST_Sold_To_Name.Text            = st.ST_Sold_To_Name;
            //ST_Sold_To_Name_2.Text          = st.ST_Sold_To_Name_2;
            ST_DEL_ST_Address.Text          = st.ST_DEL_ST_Address;
            //ST_Division.SelectedValue       = st.ST_Division;
            SetDropDownListValue(ST_Division, st.ST_Division);
            if (flg)
            {
                ST_Division_SelectedIndexChanged(null, null);
            }
            ST_Ship_To_No.Text              = st.ST_Ship_To_No;
            ST_Project_Term.Text            = st.ST_Project_Term;
            ST_Ship_To_Name.Text            = st.ST_Ship_To_Name;
            SetDropDownListValue(ST_Country, st.ST_Country);
            //if (flg)
            //{
            //    ST_Street5_SelectedIndexChanged(null, null);
            //}
            
            SetDropDownListValue(ST_City, st.ST_City);
            SetDropDownListValue(ST_Attributes1, st.ST_Attributes1);
                
            //ST_District.Text                = st.ST_District;
            //ST_Street4.Text                 = st.ST_Street4;
            //ST_Post_Code.Text               = st.ST_Post_Code;
            ST_Address.Text                  = st.ST_Address;
            ST_Contact_Person.Text          = st.ST_Contact_Person;
            ST_Tel.Text                     = st.ST_Tel;
            //SetDropDownListValue(ST_Sales_Office, st.ST_Sales_Office);
            //if (flg)
            //{
            //    ST_Sales_Office_SelectedIndexChanged(null, null);
            //}
            ST_Region.Text = st.ST_Region;
            //SetDropDownListValue(ST_Region, st.ST_Region);
            //if (flg)
            //{
            //    ST_Region_SelectedIndexChanged(null, null);
            //}
            ST_City_Code.Text = st.ST_City_Code;
            //SetDropDownListValue(ST_City_Code, st.ST_City_Code);
            //ST_County.Text                  = st.ST_County;
            //ST_Attributes1.SelectedValue    = st.ST_Attributes1;
            SetDropDownListValue(ST_Attributes1, st.ST_Attributes1);
            //ST_Attributes4.SelectedValue    = st.ST_Attributes4;
            ST_Transportation_Zone.Text     = st.ST_Transportation_Zone;
            ST_Switch_Off_Rounding.Checked  = st.ST_Switch_Off_Rounding == "1";
            ST_Relevant_For_POD.Checked     = st.ST_Relevant_For_POD == "1";
            ST_Delivery_Priority.Text       = st.ST_Delivery_Priority;
            ST_Shipping_Condition.Text      = st.ST_Shipping_Condition;
            ST_Rebate.Checked               = st.ST_Rebate == "1";
            ST_Price_Determin.Checked       = st.ST_Price_Determin == "1";
            ST_Acct_Assgmt_Group.Text       = st.ST_Acct_Assgmt_Group;
            ST_Tax_Classification.Text      = st.ST_Tax_Classification;
            ST_Itcoterms.Text               = st.ST_Itcoterms;
            ST_Order_Combination.Checked = st.ST_Order_Combination== "1";;
            ST_Complete_Delivery_Reqd_By_Law.Checked = st.ST_Complete_Delivery_Reqd_By_Law == "1";
            //ST_Terms_Of_Payment.Text        = st.ST_Terms_Of_Payment;
            ST_Attributes_9.Text = st.ST_Attributes_9;
            //ST_Ship_To_Name2.Text           = st.ST_Ship_To_Name2;
            ST_Partial_Delivery_Per_Item.Text = st.ST_Partial_Delivery_Per_Item;
            ST_Max_Partial_Deliveries.Text  = st.ST_Max_Partial_Deliveries;
            //ST_SD_Sales_Office.Text = st.ST_SD_Sales_Office;
            //ST_SD_Region.Text = st.ST_SD_Region;
            //ST_SD_Division.Text = st.ST_SD_Division;
            //ST_SD_County_Code.Text = st.ST_SD_County_Code;
        }

        public void Collect(int Index)
        {
            if (Index == -1 || ShipTos.Count == 0)
            {
                return;
            }

            hcmd_ShipTo st = ShipTos[Index];

            st.Sheets                       = Sheets.SelectedValue;
            st.ST_Sold_To_No                = ST_Sold_To_No.Text;
            //st.ST_Total_ST                  = ST_Total_ST.Text;
            st.ST_Sold_To_Name              = ST_Sold_To_Name.Text;
            //st.ST_Sold_To_Name_2            = ST_Sold_To_Name_2.Text;
            st.ST_DEL_ST_Address            = ST_DEL_ST_Address.Text;
            st.ST_Division                  = ST_Division.SelectedValue;
            st.ST_Ship_To_No                = ST_Ship_To_No.Text;
            st.ST_Project_Term              = ST_Project_Term.Text;
            st.ST_Ship_To_Name              = ST_Ship_To_Name.Text;
            st.ST_Country                   = ST_Country.Text;
            st.ST_City                      = ST_City.SelectedValue;
            //st.ST_District                  = ST_District.Text;
            //st.ST_Street4                   = ST_Street4.Text;
            //st.ST_Post_Code                 = ST_Post_Code.Text;
            st.ST_Address                    = ST_Address.Text;
            st.ST_Contact_Person            = ST_Contact_Person.Text;
            st.ST_Tel                       = ST_Tel.Text;
            //st.ST_Sales_Office              = ST_Sales_Office.Text;
            st.ST_Region                    = ST_Region.Text;
            st.ST_City_Code                 = ST_City_Code.Text;
            //st.ST_County                    = ST_County.Text;
            st.ST_Attributes1               = ST_Attributes1.SelectedValue;
            //st.ST_Attributes4               = ST_Attributes4.SelectedValue;
            st.ST_Transportation_Zone       = ST_Transportation_Zone.Text;
            st.ST_Switch_Off_Rounding       = ST_Switch_Off_Rounding.Checked ? "1" : "0";
            st.ST_Relevant_For_POD          = ST_Relevant_For_POD.Checked ? "1" : "0";
            st.ST_Delivery_Priority         = ST_Delivery_Priority.Text;
            st.ST_Shipping_Condition        = ST_Shipping_Condition.Text;
            st.ST_Rebate                    = ST_Rebate.Checked ? "1" : "0";
            st.ST_Price_Determin            = ST_Price_Determin.Checked ? "1" : "0";
            st.ST_Acct_Assgmt_Group         = ST_Acct_Assgmt_Group.Text;
            st.ST_Tax_Classification        = ST_Tax_Classification.Text;
            st.ST_Itcoterms                 = ST_Itcoterms.Text;
            st.ST_Order_Combination = ST_Order_Combination.Checked ? "1" : "0";
            st.ST_Complete_Delivery_Reqd_By_Law = ST_Complete_Delivery_Reqd_By_Law.Checked ? "1" : "0";
            //st.ST_Terms_Of_Payment          = ST_Terms_Of_Payment.Text;
            //st.ST_Ship_To_Name2             = ST_Ship_To_Name2.Text;
            st.ST_Attributes_9 = ST_Attributes_9.Text;
            st.ST_Partial_Delivery_Per_Item = ST_Partial_Delivery_Per_Item.Text;
            st.ST_Max_Partial_Deliveries    = ST_Max_Partial_Deliveries.Text;
            //st.ST_SD_Sales_Office           = ST_SD_Sales_Office.Text;
            //st.ST_SD_Region                 = ST_SD_Region.Text;
            //st.ST_SD_Division               = ST_SD_Division.Text;
            //st.ST_SD_County_Code            = ST_SD_County_Code.Text;
        }
        #endregion

        #region 方法 Save 将值循环写入数据库
        public void Save()
        {
            Collect(SheetIndex);

            //逻辑删除旧数据
            //(hcmd_ShipTo.process == TaskAPI.Process & hcmd_ShipTo.incident == TaskAPI.Incident & hcmd_ShipTo.userName == (TaskAPI.IsInitiate ? UserAPI.UserName : TaskAPI.UserName)).Edit<hcmd_ShipTo>(o =>
            //{
            //    o.Status = 0;
            //}
            //);

           // (hcmd_ShipTo.process == TaskAPI.Process & hcmd_ShipTo.incident == TaskAPI.Incident).Edit<hcmd_ShipTo>(o =>
           // {
           //     o.Status = 0;
           // }
           //);

            (hcmd_ShipTo.process == TaskAPI.Process & hcmd_ShipTo.incident == TaskAPI.Incident).Edit<hcmd_ShipTo>(o =>
            {
                o.Status = 0;
            }
           );

            //循环执行数据库写入
            int i = 1;
            foreach (hcmd_ShipTo st in ShipTos)
            {
                if (TaskAPI.IsInitiate)
                {
                    st.Process      = TaskAPI.Process;
                    st.Incident     = TaskAPI.Incident;
                    st.UserName     = UserAPI.UserName;
                    st.UserFullName = UserAPI.UserFullName;
                }
                st.Time         = System.DateTime.Now;
                st.Status       = 1;
                st.Sheets = i.ToString();
                Storage.Save(st);
                i++;                
            }
            StoreAPI.ClearKey("TabShipTo~" + ID);
        }
        #endregion

        #region 方法 InitDropdownList
        private void InitDropDownList()
        {
            if (!IsLoaded)
            {
                #region Attributes_1
                ST_Attributes1.Items.Clear();
                ST_Attributes1.Items.Add(new ListItem("", ""));
                IList<m_HK_Attributes1> sa1 = (m_Attributes_1.status == 1).List<m_HK_Attributes1>();
                foreach (m_HK_Attributes1 SA in sa1)
                {
                    ST_Attributes1.Items.Add(new ListItem(SA.Attributes_1, SA.Attributes_1));
                }
                #endregion
            }

            //#region Attributes_3
            //ST_Attributes3.Items.Clear();
            //ST_Attributes3.Items.Add(new ListItem("", ""));
            //IList<m_Attributes_3> sta3 = (m_Attributes_3.status == 1).List<m_Attributes_3>();
            //foreach (m_Attributes_3 STA in sta3)
            //{
            //    ST_Attributes3.Items.Add(new ListItem(STA.Market_Segment, STA.Market_Segment));

            //}
            //#endregion

            //#region Attributes_4
            //ST_Attributes4.Items.Clear();
            //ST_Attributes4.Items.Add(new ListItem("", ""));
            //IList<m_Attributes_4> sta4 = (m_Attributes_4.status == 1).List<m_Attributes_4>();
            //foreach (m_Attributes_4 STA in sta4)
            //{
            //    ST_Attributes4.Items.Add(new ListItem(STA.Sub_Market, STA.Sub_Market));

            //}
            //#endregion

            //#region Ship Region
            //ST_Country.Items.Clear();
            //ST_Country.Items.Add(new ListItem("", ""));
            //IList<m_Region_City> ns = (m_Region_City.status == 1).List<m_Region_City>();
            //foreach (m_Region_City NS in ns)
            //{
            //    if (ST_Country.Items.FindByValue(NS.Region) == null)
            //    {
            //        ST_Country.Items.Add(new ListItem(NS.Region, NS.Region));
            //    }
            //}
            //#endregion

        }
        #endregion

        #region 方法 SetDefaultValue 设置默认值
        public void SetDefaultValue()
        {
            ST_DEL_ST_Address.Text = "Yes";
            ST_Switch_Off_Rounding.Checked = true;
            ST_Relevant_For_POD.Checked = false;
            ST_Delivery_Priority.Text = "15";
            ST_Shipping_Condition.Text = "01";
            ST_Rebate.Checked = true;
            ST_Price_Determin.Checked = true;
            ST_Acct_Assgmt_Group.Text = "01";
            ST_Tax_Classification.Text = "0";
            ST_Itcoterms.Text = "Z01";
            ST_Order_Combination.Checked = false;
            ST_Partial_Delivery_Per_Item.Text = "C";
            ST_Max_Partial_Deliveries.Text = "";
            ST_Complete_Delivery_Reqd_By_Law.Checked = true;

            SetEnable(false, ST_Order_Combination, ST_Complete_Delivery_Reqd_By_Law,ST_DEL_ST_Address, ST_Partial_Delivery_Per_Item, ST_Switch_Off_Rounding, ST_Max_Partial_Deliveries, ST_Relevant_For_POD, ST_Delivery_Priority, ST_Shipping_Condition, ST_Rebate, ST_Price_Determin, ST_Acct_Assgmt_Group, ST_Tax_Classification, ST_Itcoterms);
            SetForeColor(Color.Orange, ST_Order_Combination_L, ST_Complete_Delivery_Reqd_By_Law_L,ST_DEL_ST_Address_L, ST_Partial_Delivery_Per_Item_L, ST_Switch_Off_Rounding_L, ST_Max_Partial_Deliveries_L, ST_Relevant_For_POD_L, ST_Delivery_Priority_L, ST_Shipping_Condition_L, ST_Rebate_L, ST_Price_Determin_L, ST_Acct_Assgmt_Group_L, ST_Tax_Classification_L, ST_Itcoterms_L);

        }
        #endregion

        #region 方法 DataValidation
        public List<string> DataValidation()
        {
            Collect(SheetIndex);

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            int Sheet = 1;

            foreach(hcmd_ShipTo st in ShipTos)
            {
                if (Keys.Items[0].Selected && Keys.Items[1].Selected && Actions.SelectedValue == "1")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_Name, string.Format("Ship to (S{0}): Sold to Name 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Division, string.Format("Ship to (S{0}): Division 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Country, string.Format("Ship to (S{0}):  Country 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_City, string.Format("Ship to (S{0}): City & District 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes1, string.Format("Ship to (S{0}): Attributes1 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Post_Code, string.Format("Ship to (S{0}): 邮编 Post code 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Address, string.Format("Ship to (S{0}): Address 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Contact_Person, string.Format("Ship to (S{0}): 联系人Contact Person 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Tel, string.Format("Ship to (S{0}): 电话 Tel 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sales_Office, string.Format("Ship to (S{0}): Sales Office销售区域 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Region, string.Format("Ship to (S{0}): Region省份 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_City_Code, string.Format("Ship to (S{0}): City Code城市 必填", Sheet)));
                    if (st.ST_Division == "K2")
                    //if (ST_Division.SelectedValue== "K2")
                    {
                        //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes3, string.Format("Ship to (S{0}): Attributes3项目类型 必填", Sheet)));
                        //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes4, string.Format("Ship to (S{0}): Attributes4项目子类型 必填", Sheet)));
                        regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Project_Term, string.Format("Ship to (S{0}): 项目简称 必填", Sheet)));
                    }
                }

                if (Keys.Items[1].Selected && Actions.SelectedValue == "1" && (!Keys.Items[0].Selected))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_No, string.Format("Ship to (S{0}): Sold to No. 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_Name, string.Format("Ship to (S{0}): Sold to Name 必填", Sheet)));
                  
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Division, string.Format("Ship to (S{0}): Division 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Country, string.Format("Ship to (S{0}): Country 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes1, string.Format("Ship to (S{0}): Attributes1 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_City, string.Format("Ship to (S{0}): City & District 必填", Sheet)));
                    
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Address, string.Format("Ship to (S{0}): Address 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Contact_Person, string.Format("Ship to (S{0}): Contact Person 必填", Sheet)));

                    if (st.ST_Division == "K2")
                    {
                        //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes3, string.Format("Ship to (S{0}): Attributes3项目类型 必填", Sheet)));
                        //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Attributes4, string.Format("Ship to (S{0}): Attributes4项目子类型 必填", Sheet)));
                        regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Project_Term, string.Format("Ship to (S{0}): Name of Project Item 必填", Sheet)));
                    }
                }

                if (Keys.Items[1].Selected && Actions.SelectedValue != "1")
                {
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_Name, string.Format("Ship to (S{0}): Sold to Name公司名称 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Division, string.Format("Ship to (S{0}): Division业务类型 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_No, string.Format("Ship to (S{0}): Sold to No. 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Ship_To_No, string.Format("Ship to (S{0}): Ship to No.送货地址编号 必填", Sheet)));
                    regs.Add(new Regular(RegularExp.NotEmpty, st.ST_Sold_To_Name, string.Format("Ship to (S{0}): Sold to Name 必填", Sheet)));
                    //regs.Add(new Regular(RegularExp.NotEmpty, st.ST_SD_Sales_Office, string.Format("Ship to (S{0}): 请输入正确的Sold to No.客户编号", Sheet)));
                }

                Sheet ++;
            }

            vald_s = Validation(regs);
            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>Ship To </b></font>");
            }

            return vald_s;
        }
        #endregion

        #region 方法 SetColor 设置颜色
        public void SetColor()
        {
            SetForeColor(Color.Black, ST_Attributes1_L,ST_City_L, ST_Sold_To_Name_L, ST_Division_L, ST_Country_L, ST_Address_L, ST_Contact_Person_L, ST_Sold_To_No_L, ST_Ship_To_No_L);
            switch (Actions.SelectedValue)
            {
                case "1":
                    SetForeColor(Color.Blue, ST_Attributes1_L,ST_City_L, ST_Sold_To_Name_L, ST_Division_L, ST_Country_L, ST_Address_L, ST_Contact_Person_L);
                    break;

                case "2":
                    SetForeColor(Color.Blue, ST_Sold_To_No_L,ST_Sold_To_Name_L);
                    break;

                case "3":
                    SetForeColor(Color.Blue, ST_Sold_To_No_L, ST_Sold_To_Name_L);
                    break;

                case "4":
                    SetForeColor(Color.Blue, ST_Sold_To_No_L, ST_Sold_To_Name_L);
                    break;
            }
        }
        #endregion

        #region 方法 SetDropDownListValue 设置下拉列表框值
        private void SetDropDownListValue(Anthem.DropDownList drpT, object o)
        {
            try
            {
                if (o == null)
                {
                    drpT.SelectedIndex = -1;
                    return;
                }

                if ((TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) || drpT.Items.FindByValue(o.ToString()) != null)
                {
                    drpT.SelectedValue = o.ToString();
                    return;
                }

                drpT.Items.Add(new ListItem(o.ToString(), o.ToString()));
                drpT.SelectedIndex = 0;

            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region 方法 Sheets 复制
        private hcmd_ShipTo CopyFrom(hcmd_ShipTo st)
        {
            hcmd_ShipTo _st = new hcmd_ShipTo();
            _st.ST_Sold_To_No = st.ST_Sold_To_No;
            //_st.ST_Hierarchy_2 = st.ST_Hierarchy_2;
            //_st.ST_Total_ST = st.ST_Total_ST;
            _st.ST_Sold_To_Name = st.ST_Sold_To_Name;
            //_st.ST_Sold_To_Name_2 = st.ST_Sold_To_Name_2;
            _st.ST_DEL_ST_Address = st.ST_DEL_ST_Address;
            //_st.ST_Division = st.ST_Division;
            //_st.ST_Ship_To_No = st.ST_Ship_To_No;
            //_st.ST_Project_Term = st.ST_Project_Term;
            //_st.ST_Ship_To_Name = st.ST_Ship_To_Name;
            //_st.ST_Country = st.ST_Country;
            //_st.ST_City = st.ST_City;
            //_st.ST_District = st.ST_District;
            //_st.ST_Street4 = st.ST_Street4;
            //_st.ST_Post_Code = st.ST_Post_Code;
            //_st.ST_Address = st.ST_Address;
            //_st.ST_Contact_Person = st.ST_Contact_Person;
            //_st.ST_Tel = st.ST_Tel;
            //_st.ST_Sales_Office = st.ST_Sales_Office;
            //_st.ST_Region = st.ST_Region;
            //_st.ST_City_Code = st.ST_City_Code;
            //_st.ST_County = st.ST_County;
            //_st.ST_Attributes3 = st.ST_Attributes3;
            //_st.ST_Attributes4 = st.ST_Attributes4;
            //_st.ST_Transportation_Zone = st.ST_Transportation_Zone;
            _st.ST_Switch_Off_Rounding = st.ST_Switch_Off_Rounding;
            _st.ST_Relevant_For_POD = st.ST_Relevant_For_POD;
            _st.ST_Delivery_Priority = st.ST_Delivery_Priority;
            _st.ST_Shipping_Condition = st.ST_Shipping_Condition;
            _st.ST_Rebate = st.ST_Rebate;
            _st.ST_Price_Determin = st.ST_Price_Determin;
            _st.ST_Acct_Assgmt_Group = st.ST_Acct_Assgmt_Group;
            _st.ST_Tax_Classification = st.ST_Tax_Classification;
            _st.ST_Itcoterms = st.ST_Itcoterms;
            _st.ST_Order_Combination = st.ST_Order_Combination;
            _st.ST_Complete_Delivery_Reqd_By_Law = st.ST_Complete_Delivery_Reqd_By_Law;
            //_st.ST_Terms_Of_Payment = st.ST_Terms_Of_Payment;
            //_st.ST_Ship_To_Name2 = st.ST_Ship_To_Name2;
            _st.ST_Partial_Delivery_Per_Item = st.ST_Partial_Delivery_Per_Item;
            _st.ST_Max_Partial_Deliveries = st.ST_Max_Partial_Deliveries;
            //_st.ST_SD_Sales_Office = st.ST_SD_Sales_Office;
            //_st.ST_SD_Region = st.ST_SD_Region;
            //_st.ST_SD_Division = st.ST_SD_Division;
            //_st.ST_SD_County_Code = st.ST_SD_County_Code;

            return _st;
        }
        #endregion

        #region City和TransportationZone 下拉框选中及赋值
        protected void ST_Country_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ST_Country.SelectedValue)
            {
                case "HK":
                    ST_City_Code.Text = "K683";
                    ST_Transportation_Zone.Text = "HKOTH00000";
                    break;

                case "MO(MACAU)":
                    ST_City_Code.Text = "K684";
                    ST_Transportation_Zone.Text = "MOOTH00000";
                    break;

                default:
                    ST_City_Code.Text = "";
                    ST_Transportation_Zone.Text = "";
                    break;
            }
        }
        #endregion

        #region City和Region联动选中及赋值
        protected void ST_City_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ST_City.SelectedValue)
            {
                case "HK Island":
                    ST_Region.Text = "HK";
                    ST_Attributes_9.Text = "330";
                    break;

                case "Kowloon":
                    ST_Region.Text = "KLN";
                    ST_Attributes_9.Text = "340";
                    break;

                case "New Territories":
                    ST_Region.Text = "NT";
                    ST_Attributes_9.Text = "350";
                    break;

                case "Macau":
                    ST_Region.Text = "MO";
                    ST_Attributes_9.Text = "360";
                    break;

                default:
                    ST_Region.Text = "";
                    ST_Attributes_9.Text = "";
                    break;
            }
        }
        #endregion

        //protected void ST_Street5_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    //ST_City.Items.Clear();
        //    //ST_City.Items.Add(new ListItem("", ""));
        //    //IList<m_Region_City> bes1 = (m_Region_City.status == 1 & (m_Region_City.region == ST_Country.SelectedValue
        //    //    | m_Region_City.region == "")).List<m_Region_City>();
        //    //foreach (m_Region_City BE in bes1)
        //    //{
        //    //    if (ST_City.Items.FindByValue(BE.City) == null)
        //    //    {
        //    //        ST_City.Items.Add(new ListItem(BE.City, BE.City));

        //    //    }
        //    //}

        //    IList<m_Region_City> mrc5 = (m_Region_City.status == 1 & (m_Region_City.region == ST_Country.SelectedValue
        //         | m_Region_City.region == "")).List<m_Region_City>();
        //    foreach (m_Region_City MR in mrc5)
        //    {
        //        ST_Post_Code.Text = MR.PostCode;

        //    }
        //}


    }
}
